import request from "@/utils/http"
import * as types from "@/store/actionTypes"

const setChannel = (payload) => ({ type: types.SETCHANNEL, payload })

const setArticle = (payload) => ({ type: types.SETARTICLE, payload })
// 获取频道
export const getChannels = () => {
  return async (dispatch) => {
    // 异步请求
    const { channels } = await request({
      url: "/channels",
    })
    // 派发action
    dispatch(setChannel(channels))
  }
}
// 获取文章列表

export const getArticles = (params) => {
  return async (dispatch) => {
    const data = await request({
      url: "/mp/articles",
      params, // query查询参数
    })
    dispatch(setArticle(data))
  }
}
// 删除文章action

export const delArticle = (id) => {
  return async (dispatch) => {
    await request({
      method: "delete",
      url: `/mp/articles/${id}`,
    })
  }
}
// 发布文章action
export const addArticle = (data, draft) => {
  return async (dispatch) => {
    await request({
      method: "post",
      url: "/mp/articles",
      params: {
        draft,
      },
      data,
    })
  }
}
// 获取文章详情
export const getArticleInfo = (id) => {
  return async () => {
    const data = await request({
      url: `mp/articles/${id}`,
    })
    return data
  }
}
// 编辑文章
export const editArticle = (data, draft) => {
  return async () => {
    await request({
      url: `mp/articles/${data.id}`,
      method: "put",
      params: {
        draft,
      },
      data,
    })
  }
}
